package com.zjx.reggie.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.reggie.common.R;
import com.zjx.reggie.dto.OrdersDto;
import com.zjx.reggie.entity.OrderDetail;
import com.zjx.reggie.entity.Orders;
import com.zjx.reggie.service.OrderDetailService;
import com.zjx.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrdersController {

    @Autowired
    OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;


    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpServletRequest request) {
        String userId = request.getSession().getAttribute("userId").toString();
        if (ordersService.addOrder(userId, orders)) {
            return R.success(null);
        }
        return R.error("失败");
    }

    @GetMapping("/userPage")
    public R pageUser(int page, int pageSize, HttpServletRequest request) {
        //Orders表分页
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        Object userId = request.getSession().getAttribute("userId");
        qw.eq(Orders::getUserId, userId);
        qw.orderByDesc(Orders::getCheckoutTime);
        Page<Orders> page1 = ordersService.page(ordersPage, qw);
        Page<OrdersDto> orderDetail = new Page<>(page, pageSize);
        BeanUtils.copyProperties(ordersPage, orderDetail);
        List<Orders> records = page1.getRecords();
        Set<String> collect = records.stream().map(item -> {
            String id = item.getId();
            return id;
        }).collect(Collectors.toSet());
        if(collect.size() != 0){
            LambdaQueryWrapper<OrderDetail> qe = new LambdaQueryWrapper<>();
            qe.in(OrderDetail::getOrderId, collect);
            List<OrderDetail> orderDetails = orderDetailService.list(qe);

            List<OrdersDto> collect1 = records.stream().map(item -> {
                OrdersDto orderDto = new OrdersDto();
                BeanUtils.copyProperties(item, orderDto);
                for (OrderDetail o : orderDetails) {
                    if (orderDto.getId().equals(o.getOrderId().toString())) {
                        orderDto.setName(o.getName());
                        orderDto.setSumNum(o.getNumber());
                    }
                }
                orderDto.setOrderDetails(orderDetails);
                return orderDto;
            }).collect(Collectors.toList());
            orderDetail.setRecords(collect1);
        }

        return R.success(orderDetail);
    }

    @GetMapping("page")
    public R page(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        //2022-05-03 00:00:00
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM:ss");
        IPage<Orders> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        if (beginTime != null && beginTime.length() != 0 && endTime != null && endTime.length() != 0) {
            wrapper.between(Orders::getOrderTime, beginTime, endTime);
        }
        wrapper.like(StrUtil.isNotBlank(number), Orders::getNumber, number);
        wrapper.orderByAsc(Orders::getOrderTime);
        IPage<Orders> page1 = ordersService.page(iPage, wrapper);
        return R.success(page1);
    }


    @PutMapping
    public R update(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改状态成功！");
    }
}
